package com.ruoyi.match.job;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.match.job.service.HalfCourtMatchService;
import com.ruoyi.match.match.domain.MatchCollectionDate;
import com.ruoyi.match.match.mapper.MatchCollectionDateMapper;
import com.ruoyi.match.utils.BrowserUtil;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.openqa.selenium.WebDriver;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * @author sjz
 * @version 1.0.0
 * @desc
 * @date 2023/11/7 20:11
 **/

@Slf4j
@Component
@RequiredArgsConstructor
public class HalfCourtMatchTask {

    private final MatchCollectionDateMapper collectionDateMapper;

    private final HalfCourtMatchService halfCourtMatchService;

    @XxlJob("halfCourtMatchTask")
    private void process() throws Exception {
        List<MatchCollectionDate> matchCollectionDates = collectionDateMapper.selectList(Wrappers.<MatchCollectionDate>lambdaQuery().eq(MatchCollectionDate::getHalfCount,Boolean.FALSE));
        if(CollUtil.isNotEmpty(matchCollectionDates)){
            WebDriver browser = BrowserUtil.createBrowser();
            try {
                for (MatchCollectionDate matchCollectionDate : matchCollectionDates) {
                    LocalDate localDate = DateUtil.toLocalDateTime(matchCollectionDate.getMatchDate()).toLocalDate();
                    halfCourtMatchService.halfCourtMatchHandler(browser,localDate);
                    matchCollectionDate.setHalfCount(Boolean.TRUE);
                    collectionDateMapper.updateById(matchCollectionDate);
                    log.info("执行日期:{} 结束",localDate);
                }
            } finally {
                log.info("浏览器退出");
                browser.quit();
            }
        }



    }
}
